package mn.idax.exchange.otc.util;

import mn.idax.exchange.common.CalendarUtil;
import mn.idax.exchange.otc.constant.CommonConstant;
import org.apache.commons.lang3.StringUtils;

import java.sql.Timestamp;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.TimeZone;

public class DateUtils {

    public static final String T_YYYY_MM_DD_HH_MM_SS = "yyyy-MM-dd HH:mm:ss";
    public static final String YYYY_MM_DD_T_HH_MM_SS = "yyyy-MM-dd'T'HH:mm:ss";

    /**
     * getFormate
     *
     * @param date
     * @return
     */
    public static String getFormate(Date date) {

        return new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(date);
    }


    /**
     * 将Date 转为 String  有误，使用下面的
     *
     * @param date
     * @param pattern
     * @return
     */
    public static String getDateToStr(Date date, String pattern) {
        String dateStr = null;
        try {
            DateFormat format = new SimpleDateFormat(pattern);
            dateStr = format.format(date);
        } catch (Exception e) {
            LogUtils.error(DateUtils.class,"getDateToStr",e);
        }
        return dateStr;
    }

    /**
     *
     * @param date
     * @param pattern
     * @return
     */
    public static String getDateToStrCorrect(Date date, String pattern) {
        String dateStr = null;
        try {
            DateFormat format = new SimpleDateFormat(pattern);
            format.setTimeZone(TimeZone.getTimeZone("UTC"));
            dateStr = format.format(date);
        }catch (Exception e){
            e.printStackTrace();
        }
        return dateStr;
    }

    /**
     * Date 转 timeStamp 有误，使用下面的
     * @param date
     * @return
     */
    public static String getDateToTimeStamp(Date date){
        return DateUtils.getDateToStr(date, CommonConstant.DATE_FORMAT_PATTREN);
    }

    public static String getDateToTimeStampCorrect(Date date){
        return DateUtils.getDateToStrCorrect(date, CommonConstant.DATE_FORMAT_PATTREN);
    }

    /**
     * 将String 转为Timestamp
     *
     * @param strDate
     * @param dateFormat
     * @return
     */
    public static Timestamp getStrToTimeStamp(String strDate, String dateFormat) {
        Timestamp timestamp = null;
        try {
            DateFormat sf = new SimpleDateFormat(dateFormat);
            Date date = sf.parse(strDate);
            timestamp = new Timestamp(date.getTime());
        } catch (Exception e) {
            LogUtils.error(DateUtils.class,"getStrToTimeStamp",e);
        }
        return timestamp;
    }

    public static Date addDay(Date date, int addDay) {
        Calendar ca = Calendar.getInstance();
        ca.setTime(date);
        ca.add(Calendar.DATE, addDay);// num为增加的天数，可以改变的
        return ca.getTime();
    }


    /**
     * @param strDate
     * @return
     */
    public static Date getDate(String strDate) {
        try {
            DateFormat dateFormat2 = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss");
            return dateFormat2.parse(strDate);
        } catch (Exception e) {
            LogUtils.error(DateUtils.class,"getDate",e);
        }
        return null;
    }



    public static Date getDate(String strDate, String pattern) {
        try {
            DateFormat dateFormat2 = new SimpleDateFormat(pattern);
            return dateFormat2.parse(strDate);
        } catch (Exception e) {
            LogUtils.error(DateUtils.class,"getDate",e);
        }
        return null;
    }


    public static Date getDate(Long time, String pattern) {
        try {
            DateFormat dateFormat2 = new SimpleDateFormat(pattern);
            String format = dateFormat2.format(new Date(time));
            return dateFormat2.parse(format);
        } catch (Exception e) {
            LogUtils.error(DateUtils.class,"getDate",e);
        }
        return null;
    }



    /**
     * Calendar转化为Date
     *
     * @param calendar
     * @return
     */
    public static Date getCalendarToDate(Calendar calendar) {
        return Calendar.getInstance().getTime();
    }

    public static Date getUTCNow() {
        return CalendarUtil.getUTCDate(new Date()).getTime();
    }

    public static String subUTCDate(Date date1,Date date2,String dateFormat){
        long startTime = date1.getTime();
        long endTime = date2.getTime();
        long between = startTime - endTime;

        SimpleDateFormat sdf = new SimpleDateFormat(dateFormat);

        if (between < 0){
            between = 0;
        }
        return sdf.format(new Date(Long.valueOf(between)));
    }
    
    public static void main(String[] args){
        String timeByTimeStamp = getTimeByTimeStamp(1558925295297L);
        String utcDateTwoMonthsAgo = getUtcDateTwoMonthsAgo();
        System.out.println(utcDateTwoMonthsAgo);
    }

    /**
     * TimestampToDate
     * @param dateFormat
     * @param longDate
     * @return
     */
    public static Date getTimestampToDate(String dateFormat,long longDate){
        Date date=null;
        try {
            SimpleDateFormat sf = new SimpleDateFormat(dateFormat);
            sf.setTimeZone(TimeZone.getTimeZone("UTC"));
            String dateStr = sf.format(new Date(longDate));
            LogUtils.info(DateUtils.class,"====getTimestampToDate======dateStr=========:"+dateStr);

            date = sf.parse(dateStr);
            LogUtils.info(DateUtils.class,"====getTimestampToDate======date=========:"+date);
        }catch (Exception e){
            e.printStackTrace();
        }
        return date;
    }

    /**
     * 开始时间
     * @param startDate
     * @return
     */
    public static Long getStartDate(Long startDate){
        //30天(d)=2592000000毫秒(ms);
        Long historyDay=new Long("2592000000");
        if(startDate == null || startDate==0){
            startDate=System.currentTimeMillis()-historyDay+6000;
        }
        return startDate;
    }

    /**
     * 结束时间
     * @param endDate
     * @return
     */
    public static Long getEndDate(Long endDate){

        if(endDate == null || endDate==0){
            endDate=System.currentTimeMillis();
        }
        return endDate;
    }


    public static String getStartTime(String startTime){
        if(StringUtils.isNotBlank(startTime)){
            Date date = new Date(Long.parseLong(startTime));
            return DateUtils.getUtcDateStr(date);
        }
        return startTime;
    }


    public static String getEndTime(String endTime){
        if(StringUtils.isNotBlank(endTime)){
            Date date = new Date(Long.parseLong(endTime));
            date = DateUtils.addDay(date,1);
            return DateUtils.getUtcDateStr(date);
        }
        return endTime;
    }


    public static String getUtcDateStr(Date date) {
        DateFormat dateFormat = new SimpleDateFormat(DateUtils.T_YYYY_MM_DD_HH_MM_SS);
        dateFormat.setTimeZone(TimeZone.getTimeZone("UTC"));
        return dateFormat.format(date);
    }


    public static String getTimeByTimeStamp(long time) {
        DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss");
        Date date = new Date(time);
        return dateFormat.format(date);
    }

    /**
     *
     * @param sendTime
     * @return
     */
    public static long formateTimeToTimestamp(String sendTime) {

        DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss");
        Date parse = null;
        try {
            parse = dateFormat.parse(sendTime);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return parse.getTime();
    }

    /**
     * 获取utc7天前的时间
     * @param
     * @return
     */
    public static String getUtcDateSevenDayAgo() {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(getUTCNow());
        calendar.add(Calendar.DAY_OF_MONTH, -7);
        DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss");
        return  dateFormat.format( calendar.getTime());
    }


    /**
     * 获取utc两个月前的时间
     * @param
     * @return
     */
    public static String getUtcDateTwoMonthsAgo() {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(getUTCNow());
        calendar.add(Calendar.MONTH, -2);
        DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss");
        return  dateFormat.format( calendar.getTime());
    }

}
